package cn.vonce.sqlbean.example.controller;

import cn.vonce.sql.helper.Cond;
import cn.vonce.sql.helper.Wrapper;
import cn.vonce.sqlbean.example.common.Result;
import cn.vonce.sqlbean.example.model.BaseEntity;
import cn.vonce.sqlbean.example.model.entity.User;
import cn.vonce.sqlbean.example.service.UserService;
import cn.vonce.supercode.core.config.GenerateConfig;
import cn.vonce.supercode.core.helper.GenerateHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 表操作 控制器
 *
 * @author Jovi
 * @email imjovi@qq.com
 * @date 2024/2/27 15:18
 */
@RequestMapping
@RestController
public class DbManageController {

    @Autowired
    private UserService userService;

    /**
     * 备份表数据
     *
     * @return
     */
    @PostMapping("backup")
    public Result backup() {
        userService.backup();
        return Result.success();
    }

    /**
     * 备份表数据
     *
     * @param newName
     * @return
     */
    @PostMapping("backup2")
    public Result backup2(String newName) {
        userService.backup(newName);
        return Result.success();
    }

    /**
     * 备份表数据
     *
     * @param schema
     * @param newName
     * @return
     */
    @PostMapping("backup3")
    public Result backup3(String schema, String newName) {
        userService.backup(schema, newName);
        return Result.success();
    }

    /**
     * 备份表数据
     *
     * @param schema
     * @param newName
     * @return
     */
    @PostMapping("backup4")
    public Result backup4(String schema, String newName) {
        userService.backup(Wrapper.where(Cond.gt(User::getAge, 5)), schema, newName);
        return Result.success();
    }

    /**
     * 备份表数据
     *
     * @param newName
     * @return
     */
    @PostMapping("backup5")
    public Result backup5(String newName) {
        userService.backup(Wrapper.where(Cond.gt(User::getAge, 5)), newName, User::getId, User::getUserName, User::getMobilePhone);
        return Result.success();
    }

    /**
     * 复制表数据到同样结构的表
     *
     * @param newName
     * @return
     */
    @PostMapping("copy")
    public Result copy(String newName) {
        userService.copy(Wrapper.where(Cond.gt(User::getAge, 5)), newName);
        return Result.success();
    }

    /**
     * 复制表数据到同样结构的表
     *
     * @param schema
     * @param newName
     * @return
     */
    @PostMapping("copy2")
    public Result copy2(String schema, String newName) {
        userService.copy(Wrapper.where(Cond.eq(User::getAge, 1)), schema, newName);
        return Result.success();
    }

    /**
     * 复制表数据到同样结构的表
     *
     * @param schema
     * @param newName
     * @return
     */
    @PostMapping("copy3")
    public Result copy3(String schema, String newName) {
        userService.copy(Wrapper.where(Cond.eq(User::getAge, 1)), schema, newName, User::getId, User::getUserName, User::getMobilePhone);
        return Result.success();
    }

    /**
     * 删除表
     *
     * @return
     */
    @GetMapping("dropTable")
    public Result dropTable() {
        userService.dropTable();
        return Result.success();
    }

    /**
     * 创建表
     *
     * @return
     */
    @PostMapping("createTable")
    public Result createTable() {
        userService.createTable();
        return Result.success();
    }

    /**
     * 删除表并同时创建表
     *
     * @return
     */
    @PostMapping("dropAndCreateTable")
    public Result dropAndCreateTable() {
        userService.dropAndCreateTable();
        return Result.success();
    }

    /**
     * 修改表备注
     *
     * @param remarks
     * @return
     */
    @GetMapping("alterRemarks")
    public Result alterRemarks(String remarks) {
        userService.alterRemarks(remarks);
        return Result.success();
    }

    /**
     * 查询表信息
     *
     * @return
     */
    @GetMapping("getTableList")
    public Result getTableList() {
        return Result.success(userService.getTableList());
    }

    /**
     * 查询表信息
     *
     * @param tableName
     * @return
     */
    @GetMapping("getTableList2")
    public Result getTableList2(String tableName) {
        return Result.success(userService.getTableList(tableName));
    }

    /**
     * 查询表信息
     *
     * @param schema
     * @param tableName
     * @return
     */
    @GetMapping("getTableList3")
    public Result getTableList3(String schema, String tableName) {
        return Result.success(userService.getTableList(schema, tableName));
    }

    /**
     * 查询表结构
     *
     * @return
     */
    @GetMapping("getColumnInfoList")
    public Result getColumnInfoList() {
        return Result.success(userService.getColumnInfoList());
    }

    /**
     * 查询表结构
     *
     * @param tableName
     * @return
     */
    @GetMapping("getColumnInfoList2")
    public Result getColumnInfoList2(String tableName) {
        return Result.success(userService.getColumnInfoList(tableName));
    }

    /**
     * 查询表结构
     *
     * @param schema
     * @param tableName
     * @return
     */
    @GetMapping("getColumnInfoList3")
    public Result getColumnInfoList3(String schema, String tableName) {
        return Result.success(userService.getColumnInfoList(schema, tableName));
    }

    /**
     * 获取Schemas列表
     *
     * @param name
     * @return
     */
    @GetMapping("getSchemas")
    public Result getSchemas(String name) {
        List<String> databases = userService.getSchemas(name);
        return Result.success("获取Schemas列表成功", databases);
    }

    /**
     * 创建Schema
     *
     * @param name
     * @return
     */
    @PostMapping("createSchema")
    public Result createSchema(String name) {
        userService.createSchema(name);
        return Result.success("创建Schema成功");
    }

    /**
     * 删除Schema
     *
     * @param name
     * @return
     */
    @DeleteMapping("dropSchema")
    public Result dropSchema(String name) {
        userService.dropSchema(name);
        return Result.success("删除Schema成功");
    }

    /**
     * 生成代码
     *
     * @return
     */
    @PostMapping("buildCode")
    public Result buildCode() {
        //生成配置
        GenerateConfig config = new GenerateConfig();
        config.setAuthor("Jovi");
        config.setEmail("imjovi@qq.com");
        config.setBasePackage("com.vonce.test");
        //设置实体类继承的父类
        config.setBaseClass(BaseEntity.class);
        config.setPrefix("t_");
//        config.setMultiProject(true);
        //代码生成
        GenerateHelper.build(config, userService);
        return Result.success();
    }


}
